package com.jcz.rentDaoImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.jcz.entity.House;
import com.jcz.entity.HouseCondition;
import com.jcz.rentDao.HibernateUtil;
import com.jcz.rentDao.HouseDao;


public class HouseDaoImpl implements HouseDao {

	@Override
	public List<House> getAllHouse() {
		// TODO Auto-generated method stub
		
		List<House> hs = new ArrayList<House>();
		try {
			Session session = HibernateUtil.currentSession();
			Query query = session
					.createQuery("from House h join fetch h.types join fetch h.street");
			hs = query.list();
			// House h=(House)session.get(House.class, 1);
			// hs.add(h);
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession();
		}

		return hs;
	}

	@Override
	public List<House> getHouseByCondition(HouseCondition condition) {
		// TODO Auto-generated method stub
		List<House> hs = null;
		try {
			Session session = HibernateUtil.currentSession();
			Criteria cri = session.createCriteria(House.class);
			if (condition != null) {
				// ����HouseCondition�����Ƿ�Ϊnull,��� ��ѯ����
				if (condition.getTitle() != null) {
					cri.add(Restrictions.ilike("title", condition.getTitle()));
				}
				if (condition.getMinPrice() != null) {
					// hql += "and price>=:minPrice ";
					cri.add(Restrictions.ge("price", condition.getMinPrice()));
				}
				if (condition.getMaxPrice() != null) {
					// hql += "and price<=:maxPrice ";
					cri.add(Restrictions.le("price", condition.getMaxPrice()));
				}
				if (condition.getMinFloorage() != null) {
					// hql += "and floorage>=:minFloorage ";
					cri.add(Restrictions.ge("floorage",
							condition.getMinFloorage()));
				}
				if (condition.getMaxFloorage() != null) {
					// hql += "and floorage<=:maxFloorage ";
					cri.add(Restrictions.le("floorage",
							condition.getMaxFloorage()));
				}
			}

			hs = cri.list();

			// String hql = "from House where 1=1 ";
			//
			// if (condition != null) {
			// // ����HouseCondition�����Ƿ�Ϊnull,��� ��ѯ����
			// if (condition.getTitle() != null) {
			// hql += "and title like :title ";
			// }
			// if (condition.getMinPrice() != null) {
			// hql += "and price>=:minPrice ";
			// }
			// if (condition.getMaxPrice() != null) {
			// hql += "and price<=:maxPrice ";
			// }
			// if (condition.getMinFloorage() != null) {
			// hql += "and floorage>=:minFloorage ";
			// }
			// if (condition.getMaxFloorage() != null) {
			// hql += "and floorage<=:maxFloorage ";
			// }
			// }
			//
			// Query query = session.createQuery(hql);
			//
			// query.setProperties(condition);// hql������������ֵ
			//
			// hs = query.list();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession();
		}

		return hs;
	}

	@Override
	public List<House> getHouseByConditionPage(HouseCondition condition,
			int pageIndex, int pageSize) {
		// TODO Auto-generated method stub
		List<House> hs = null;
		try {
			Session session = HibernateUtil.currentSession();
			Criteria cri = session.createCriteria(House.class)
					.createAlias("types", "type")
					.createAlias("street", "street")
					.createAlias("street.district", "d");
			if (condition != null) {
				// ����HouseCondition�����Ƿ�Ϊnull,��� ��ѯ����
				if (condition.getTitle() != null) {
					cri.add(Restrictions.ilike("title", condition.getTitle()));
				}
				if (condition.getMinPrice() != null) {
					// hql += "and price>=:minPrice ";
					cri.add(Restrictions.ge("price", condition.getMinPrice()));
				}
				if (condition.getMaxPrice() != null) {
					// hql += "and price<=:maxPrice ";
					cri.add(Restrictions.le("price", condition.getMaxPrice()));
				}
				if (condition.getMinFloorage() != null) {
					// hql += "and floorage>=:minFloorage ";
					cri.add(Restrictions.ge("floorage",
							condition.getMinFloorage()));
				}
				if (condition.getMaxFloorage() != null) {
					// hql += "and floorage<=:maxFloorage ";
					cri.add(Restrictions.le("floorage",
							condition.getMaxFloorage()));
				}
				if (condition.getStreetId() != null) {
					cri.add(Restrictions.eq("street.id",
							condition.getStreetId()));
				}
				if (condition.getTypeId() != null) {
					cri.add(Restrictions.eq("types.id", condition.getTypeId()));
				}
			}

			cri.setFirstResult((pageIndex - 1) * pageSize);
			cri.setMaxResults(pageSize);

			hs = cri.list();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession();
		}

		return hs;

	}

	@Override
	public Long getHouseCountByCondition(HouseCondition condition) {
		// TODO Auto-generated method stub
		Long count = null;
		try {
			Session session = HibernateUtil.currentSession();
			Criteria cri = session.createCriteria(House.class)
					.createAlias("types", "type")
					.createAlias("street", "street")
					.createAlias("street.district", "d");
			if (condition != null) {
				// ����HouseCondition�����Ƿ�Ϊnull,��� ��ѯ����
				if (condition.getTitle() != null) {
					cri.add(Restrictions.ilike("title", condition.getTitle()));
				}
				if (condition.getMinPrice() != null) {
					// hql += "and price>=:minPrice ";
					cri.add(Restrictions.ge("price", condition.getMinPrice()));
				}
				if (condition.getMaxPrice() != null) {
					// hql += "and price<=:maxPrice ";
					cri.add(Restrictions.le("price", condition.getMaxPrice()));
				}
				if (condition.getMinFloorage() != null) {
					// hql += "and floorage>=:minFloorage ";
					cri.add(Restrictions.ge("floorage",
							condition.getMinFloorage()));
				}
				if (condition.getMaxFloorage() != null) {
					// hql += "and floorage<=:maxFloorage ";
					cri.add(Restrictions.le("floorage",
							condition.getMaxFloorage()));
				}
				if (condition.getStreetId() != null) {
					cri.add(Restrictions.eq("street.id",
							condition.getStreetId()));
				}
				if (condition.getTypeId() != null) {
					cri.add(Restrictions.eq("types.id", condition.getTypeId()));
				}

			}

			cri.setProjection(Projections.count("id"));
			count = (Long) cri.uniqueResult();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession();
		}
		return count;

	}

}
